<?php

/**
 *  tasks
 */
class Task_base {
    public $task_id;
    public function startTask($task=''){
        $insert = "insert into task_logs (`task_name`,`create_time`) 
            values('".$task."','".date('Y-m-d H:i:s')."') ";
        $task_id = DB::query(Database::INSERT, $insert )->execute('newerp');
        if(!$task_id) {
            $this->endTask('Create task failed(Database operation failed).', 2);
        }else {
            return $task_id;
        }
    }
    public function endTask($msg = '', $code = 0){
        if($this->task_id) {
            $msg = str_replace("'", "\'", $msg);
            $update = "UPDATE task_logs SET task_info='".$msg."', task_code=" . intval($code) . 
                " WHERE id=".$this->task_id;
            $flag = DB::query(Database::UPDATE, $update)->execute('newerp');
        }
        $result = array('code' => $code, 'info' => $msg);
        $output = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
        exit($output);
    }
    public function taskLog($task, $msg = '', $code = 0){
        $insert = "insert into task_logs (`task_name`,`task_info`,`task_code`,`create_time`)
            values('".$task."','".$msg."',".intval($code).",'".date('Y-m-d H:i:s')."') ";
        $task_id = DB::query(Database::INSERT, $insert )->execute('newerp');
        if(!$task_id) {
            $this->endTask('Create task failed(Database operation failed).', 2);
        }else {
            $result = array('code' => $code, 'info' => $msg);
            $output = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
            exit($output);
        }
    }
}
